<?php 
class Medicines extends CI_Model{
	
	// get 
	function get_medicine($id){
		$result = $this->db->get_where('med_notification', array('user_id'=>$id));	
		
		if($result->num_rows()>0){
			return $result->result();
		}
	} 
	// get routine the routine using routine_id
	function get_routine($id){
		$routine = $this->db->get_where('routines', array('routine_id'=>$id));
		if($routine->num_rows()==1){
			return $routine->row();
		}
	}
	function get_med($mid){
		$med = $this->db->get_where('medicines', array('medicine_id'=>$mid));
		if($med->num_rows()==1){
			return $med->row();
		}
		else{
			return FALSE;
		}
	}
	// insert new medicine
	function insert_med($data){
		$insert = $this->db->insert('medicines', $data);
		if($insert){
			$get = $this->get_all_med();
			if($get){
				$new_med = $get->last_row();
				return $new_med->medicine_id;
			}
		}
		else{
			return FALSE;
		}
	}
	// get all medicines in the DB
	function get_all_med(){
		$this->db->order_by('medicine_id','asc');
		$med = $this->db->get('medicines');
		if($med->num_rows()>0){
			return $med;
		}
		else{
			return FALSE;
		}
	}
	// get routine_id from routine
	function get_routine_id($routine){
		$query = $this->db->get_where('routines', array('routine'=>$routine));
		if($query->num_rows()==1){
			return $query->row();
		}
	}
	//get type_id from type
	function get_type_id($type){
		$query = $this->db->get_where('med_types', array('type'=>$type));
		if($query->num_rows()==1){
			return $query->row();
		}
	}
	function get_unit_id($unit){
		$query = $this->db->get_where('units', array('unit'=>$unit));
		if($query->num_rows()==1){
			return $query->row();
		}
	}
	function get_time_id($time){
		$query = $this->db->get_where('times', array('time'=>$time));
		if($query->num_rows()==1){
			return $query->row();
		}
	}
	
	// insert medicine history of user
	function insert_user_med($data){
		$insert = $this->db->insert('user_med', $data);
		if($insert){
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	// insert medicine notification
	function insert_notify($data){
		$insert = $this->db->insert('med_notification', $data);
		if($insert){
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	// get user default time
	function get_routine_time($uid, $tid){
		$rt = $this->db->get_where('user_routine_time', array('user_id'=>$uid, 'time_id'=>$tid));
		if($rt->num_rows()==1){
			return $rt->row();
		}
		else{
			return FALSE;
		}
		
	}
	
	// get user medicines 
	function get_user_medicine($uid){
		$q = "SELECT * 
			  FROM ( 
				SELECT * 
				FROM user_med
				JOIN medicines ON user_med.med_id = medicines.medicine_id
				WHERE user_id = $uid
			  )t
			  JOIN routines ON t.routine_id = routines.routine_id
			  JOIN times ON t.time_id = times.time_id
			  JOIN med_types ON t.type_id = med_types.type_id
			  ORDER BY id";
		$user_med = $this->db->query($q);
		if($user_med->num_rows() > 0){
			return $user_med->result();
		}
		else return FALSE;
	}
	
	function get_medicine_history($uid){
		$q = "SELECT * 
			  FROM ( 
				SELECT * 
				FROM history
				JOIN medicines ON history.med_id = medicines.medicine_id
				WHERE user_id = $uid
			  )t
			  JOIN routines ON t.routine_id = routines.routine_id
			  JOIN times ON t.time_id = times.time_id
			  JOIN med_types ON t.type_id = med_types.type_id
			  ORDER BY history_id";
		$user_med = $this->db->query($q);
		if($user_med->num_rows() > 0){
			return $user_med->result();
		}
		else return FALSE;
	}
	
	function get_notify_medicine($uid, $time){
		$q = "SELECT * FROM (SELECT * FROM med_notification 
			JOIN medicines ON med_notification.med_id = medicines.medicine_id) m
			JOIN units ON m.unit_id = units.unit_id
			WHERE user_id = $uid AND notify_time = '$time'";
			
		$notify_med = $this->db->query($q);
		if($notify_med->num_rows() > 0){
			return $notify_med->result();
		}
		else return FALSE;
	}
	
	function add_history($info){
		$add = $this->db->insert('history',$info);
		if($add){
			return TRUE;
		}
		else return FALSE;
	}
	
	function update_history($cond, $status){
		$this->db->where($cond);
		$up = $this->db->update('history',array('count'=>$status, 'date_time'=>date("Y-m-d H:i:s")));
		if($up){
			return TRUE;
		}
		else return FALSE;
	}
	
	function get_history($cond){
		$get = $this->db->get_where('history', $cond);
		if($get->num_rows() == 1){
			return TRUE;
		}
		else return FALSE;
	}
}